package com.ecarx.vhaltests.hy11.testcases.climate;

import android.car.VehicleAreaType;
import android.car.VehiclePropertyIds;
import android.car.VehicleUnit;
import android.util.Log;

import com.ecarx.vhaltests.IpcpClient;
import com.ecarx.vhaltests.VehicleConstants;
import com.ecarx.vhaltests.VhalUnitTestBase;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;


@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class HvacTemperatureDisplayUnitsTest extends VhalUnitTestBase {

    private static final String TAG = "HvacTemperatureDisplayUnitsTest";

    private static final int HVAC_TEMPERATURE_DISPLAY_UNITS = VehiclePropertyIds.HVAC_TEMPERATURE_DISPLAY_UNITS;

    private static final int HVAC_TEMPERATURE_DISPLAY_UNITS_AREA = VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL;

    private static final int SERVICE_ID_CEM = 37;
    private static final int OP_ID_CEM = 999;

    private boolean isAvailable = true;

    @BeforeClass
    public static void testInit() {
        setConfig(OP_ID_CEM);
        delay();
    }

    @Test
    public void testTemperatureDisplayUnitsAvailable_01() {
        // 设置 空调显示单位 不可用
        setHvacTemperatureDisPlayUnitsAvail(!isAvailable);
        delay();
        tesTemperatureDisplayUnitsAvailable(HVAC_TEMPERATURE_DISPLAY_UNITS_AREA, false);
        delay();
    }

    @Test
    public void testTemperatureDisplayUnitsAvailable_02() {
        // 设置 空调显示单位 可用
        setHvacTemperatureDisPlayUnitsAvail(isAvailable);
        delay();
        tesTemperatureDisplayUnitsAvailable(HVAC_TEMPERATURE_DISPLAY_UNITS_AREA, true);
        delay();
    }


    @Test
    public void testTemperatureDisplayUnitsGet_01() {
        setHvacTemperatureDisPlayUnitsAvail(isAvailable);
        delay();
        // 设置 空调显示单位为摄氏度：CELSIUS
        testHvacTemperatureDisplayUnitsGet(HVAC_TEMPERATURE_DISPLAY_UNITS_AREA, VehicleUnit.CELSIUS);
        delay();
    }

    @Test
    public void testTemperatureDisplayUnitsGet_02() {
        setHvacTemperatureDisPlayUnitsAvail(isAvailable);
        delay();
        // 空调显示单位为华氏度：FAHRENHEIT
        testHvacTemperatureDisplayUnitsGet(HVAC_TEMPERATURE_DISPLAY_UNITS_AREA, VehicleUnit.FAHRENHEIT);
        delay();
    }

    @Test
    public void testTemperatureDisplayUnitsSet_01() {
        // 设置 空调显示单位 可用
        setHvacTemperatureDisPlayUnitsAvail(isAvailable);
        delay();
        //  空调显示单位为摄氏度：CELSIUS
        testHvacTemperatureDisplayUnitsSet(HVAC_TEMPERATURE_DISPLAY_UNITS_AREA, VehicleUnit.CELSIUS);
        delay();

    }

    @Test
    public void testTemperatureDisplayUnitsSet_02() {
        setHvacTemperatureDisPlayUnitsAvail(isAvailable);
        delay();
        // 空调显示单位为华氏度：FAHRENHEIT
        testHvacTemperatureDisplayUnitsSet(HVAC_TEMPERATURE_DISPLAY_UNITS_AREA, VehicleUnit.FAHRENHEIT);
        delay();
    }

    @AfterClass
    public static void testReset() {
        mCarPropertyMgr.setIntProperty(HVAC_TEMPERATURE_DISPLAY_UNITS, HVAC_TEMPERATURE_DISPLAY_UNITS_AREA, VehicleUnit.CELSIUS);
        delay();
    }

    public void tesTemperatureDisplayUnitsAvailable(int area, boolean avail) {
        boolean isAvail = mCarPropertyMgr.isPropertyAvailable(HVAC_TEMPERATURE_DISPLAY_UNITS, area);
        Log.d(TAG, "tesTemperatureDisplayUnitsAvailable isAvail：" + isAvail);
        Assert.assertEquals(avail,isAvail);
    }

    public void testHvacTemperatureDisplayUnitsSet(int area, int value) {
        Log.d(TAG, "testHvacTemperatureDisplayUnitsSet area：" + area + " value: " + value);
        mCarPropertyMgr.setIntProperty(HVAC_TEMPERATURE_DISPLAY_UNITS, area, value);
        delay();
        int hvacTemperatureDisplayUnits = mCarPropertyMgr.getIntProperty(HVAC_TEMPERATURE_DISPLAY_UNITS, area);
        Assert.assertEquals(value, hvacTemperatureDisplayUnits);
    }

    public void testHvacTemperatureDisplayUnitsGet(int area, int value) {
        Log.d(TAG, "testHvacTemperatureDisplayUnitsGet area：" + area + " value: " + value);
        mCarPropertyMgr.setIntProperty(HVAC_TEMPERATURE_DISPLAY_UNITS, area, value);
        delay();
        int hvacTemperatureDisplayUnits = mCarPropertyMgr.getIntProperty(HVAC_TEMPERATURE_DISPLAY_UNITS, area);
        Assert.assertEquals(value, hvacTemperatureDisplayUnits);
    }

    public void setHvacTemperatureDisPlayUnitsAvail(boolean avail) {
        Log.d(TAG, "setHvacTemperatureDisPlayUnitsAvail avail：" + avail);
        IpcpClient ipcpClient = new IpcpClient();
        String[] usgModeString = {"vehModMngtGlbSafe1UsgModSts"};
        boolean setCEMSignalStatus;

        if (!avail) {
            int[] vgmValuesNotAvail = {VehicleConstants.USG_MODE_ABANDON};
            setCEMSignalStatus = ipcpClient.setCEMSignal(SERVICE_ID_CEM, OP_ID_CEM, usgModeString, vgmValuesNotAvail);
        } else {
            int[] vgmValuesAvail = {VehicleConstants.USG_MODE_DRIVING};
            setCEMSignalStatus = ipcpClient.setCEMSignal(SERVICE_ID_CEM, OP_ID_CEM, usgModeString, vgmValuesAvail);
        }
        Assert.assertTrue(setCEMSignalStatus);
    }


    private  static void setConfig(int OperId) {
        IpcpClient ipcpClient = new IpcpClient();
        int climateOperId[] = {OperId};
        ipcpClient.setConfig(SERVICE_ID_CEM, climateOperId);
    }

}
